<template>
  <div class="file-preview"> </div>
</template>
<script>
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {}
  },
  mounted() {
    Bus.$on('showDialogFilePreview', (v) => {
      this.beforePreview(v)
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogFilePreview')
  },
  methods: {
    beforePreview(v) {
      let { id, attrSuffixStr, attrSize } = v
      if (attrSuffixStr == 'pdf') {
        this.handlePreview(id, v)
        return
      }
      //文件格式限制
      let supportedFile = [
        'pdf',
        'doc',
        'docx',
        'ppt',
        'pptx',
        'xls',
        'xlsx',
        'txt',
      ]
      if (!supportedFile.includes(attrSuffixStr)) {
        this.$message({
          message: '该文件格式不支持预览',
          type: 'warning',
        })
        return
      }
      // let routeUrl = this.$router.resolve({
      //   name: 'pdfPreview',
      //   query: { id },
      // })
      // window.open(routeUrl.href, '_blank')
      // return
      //文件大小提醒
      let maxSize = 5 * 1024 * 1024 //预览最大5M限制
      if (attrSize >= maxSize) {
        this.$message({
          message: '该文件过大，加载时间可能会较长',
          type: 'warning',
        })
        setTimeout(() => {
          this.handlePreview(id)
        }, 800)
      } else {
        this.handlePreview(id)
      }
    },
    handlePreview(id, pdf) {
      let _href = ''
      if (pdf) {
        _href = pdf.httpUrl
      } else {
        _href = this.baseURL + '/openApi/office/office2pdf/' + id
      }
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = _href
      link.target = '_blank'
      document.body.appendChild(link)
      link.click()
    },
  },
}
</script>